/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package formularios;

import Clases.AnchoColumna;
import Clases.AutoCompleteCombo;
import Clases.ComboBox;
import Clases.ModeloTabla;
import Clases.Tools;
import Clases.Transacciones;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.text.JTextComponent;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author Jrg
 */
public class CargoExtra extends javax.swing.JInternalFrame {

    public static int IdMenu=3;
    int _idAlumno, _idServicio;
    JTable _tabla, _tablaGrados;
    ModeloTabla _modeloTabla, _modeloTablaGrado;
    double _total;
    /**
     * Creates new form Venta
     */
    public CargoExtra() 
    {
        initComponents();
        ObtenerFechaActual();
        CargarComboAlumno();
        CargarComboArticulos();
        CrearModeloTablaGrados();
        CargarTablaGrados();
        AgregarListenerComboAlumno();
        AgregarListenerComboArticulos();
        CrearModeloTabla();
        _idAlumno=0;
    }
    
    private void ObtenerFechaActual()
    {
        jDateFecha.setDate(Transacciones.ObtenerFechaServidor(MainMenu.conexion));
    }
    
    private void CargarComboAlumno()
    {
        DefaultComboBoxModel modelo = Clases.ComboBox.CargarCombo("select idalumno id, nombre from alumnos where debaja is null order by nombre asc", MainMenu.conexion);
        cbAlumno.setModel(modelo);
        AutoCompleteDecorator.decorate(cbAlumno);
    }
    
   
    private void CargarComboArticulos()
    {
        DefaultComboBoxModel modelo = Clases.ComboBox.CargarCombo("select idarticulo id, nombre from articulos where debaja is null order by nombre asc", MainMenu.conexion);
        cbArticulos.setModel(modelo);
        AutoCompleteDecorator.decorate(cbArticulos);
    }
    
    private void AgregarListenerComboAlumno()
    {
        cbAlumno.addActionListener (new ActionListener () 
        {
            public void actionPerformed(ActionEvent e) 
            {
                try
                {
                    _idAlumno = ((ComboBox) cbAlumno.getSelectedItem()).getId();
                }
                catch(Exception ex){}
            }
        });
    }
    
    
    private void AgregarListenerComboArticulos()
    {
        cbArticulos.addActionListener (new ActionListener () 
        {
            public void actionPerformed(ActionEvent e) 
            {
                try
                {
                    _idServicio = ((ComboBox) cbArticulos.getSelectedItem()).getId();
                    CargarPrecio();
                }
                catch(Exception ex){}
            }
        });
    }
    
    private void CargarPrecio()
    {
        if (_idServicio!=0)
        {
            String precio = MainMenu.conexion.EjecutarEscalar("SELECT Round(precio,2) FROM articulos where idarticulo="+_idServicio);
            tfMonto.setText(precio);
        }
    }
    
    private void CrearModeloTabla()
    {
        try
        {
            /*columnas que no son editables*/
            Hashtable colReadOnly= new Hashtable();
            colReadOnly.clear();
            colReadOnly.put(0, 0); //IdServicio
            colReadOnly.put(1, 300); //Servicio
            colReadOnly.put(2, 200); //Monto
            String[] columnNames = {"IdServicio","Servicio","Monto"};
            final Class<?>[] columnClasses = {Integer.class, String.class, Double.class}; 
            _modeloTabla = new ModeloTabla(colReadOnly);
            _modeloTabla.setColumnIdentifiers(columnNames);
            _modeloTabla.RemoveAllRows();
            _tabla = new JTable(_modeloTabla)
            {
                @Override
                public Class<?> getColumnClass(int columnIndex)
                {
                    return columnClasses[columnIndex];
                }
            };
            AnchoColumna ac = new AnchoColumna(_tabla,colReadOnly);
//          OcultarColumnas oc = new OcultarColumnas(_tabla,0);
            JScrollPane scroll = new JScrollPane(_tabla);
            _tabla.getTableHeader().setReorderingAllowed(false);
            panelTabla.setVisible(false);
            panelTabla.add(scroll);
            panelTabla.setVisible(true);
            panelTabla.repaint();
        }
        catch(Exception ex)
        {
            
        }
    }
    
    
    
    private void CrearModeloTablaGrados()
    {
        try
        {
            /*columnas que no son editables*/
            Hashtable colReadOnly= new Hashtable();
            colReadOnly.clear();
            colReadOnly.put(0, 0); //IdGrado
            colReadOnly.put(1, 300); //NOmbre
            //colReadOnly.put(2, 100); //Seleccionar
            
            Hashtable anchoCol= new Hashtable();
            anchoCol.clear();
            anchoCol.put(0, 0); //IdGrado
            anchoCol.put(1, 300); //NOmbre
            anchoCol.put(2, 100); //Seleccionar
            String[] columnNames = {"IdGrado","Nombre", "Seleccionar"};
            final Class<?>[] columnClasses = {Integer.class,String.class, Boolean.class}; 
            _modeloTablaGrado = new ModeloTabla(colReadOnly);
            _modeloTablaGrado.setColumnIdentifiers(columnNames);
            _modeloTablaGrado.RemoveAllRows();
            _tablaGrados = new JTable(_modeloTablaGrado)
            {
                @Override
                public Class<?> getColumnClass(int columnIndex)
                {
                    return columnClasses[columnIndex];
                }
            };
            AnchoColumna ac = new AnchoColumna(_tablaGrados,anchoCol);
//          OcultarColumnas oc = new OcultarColumnas(_tabla,0);
            JScrollPane scroll = new JScrollPane(_tablaGrados);
            _tablaGrados.getTableHeader().setReorderingAllowed(false);
            PanelGrados.setVisible(false);
            PanelGrados.add(scroll);
            PanelGrados.setVisible(true);
            PanelGrados.repaint();
        }
        catch(Exception ex)
        {
            
        }
    }
    
    private void CargarTablaGrados()
    {
        try
        {
            _modeloTablaGrado.RemoveAllRows();
            ResultSet rs = MainMenu.conexion.EjecutarConsulta("select idgrado, nombre from grados where debaja is null order by idgrado");
            while (rs.next())
            {
                Vector alumno = new Vector();
                alumno.addElement(rs.getInt("idgrado"));//0
                alumno.addElement(rs.getString("nombre"));//1
                alumno.addElement(true);//2
                _modeloTablaGrado.addRow(alumno);
            }
        }
        catch(Exception ex)
        {
            
        }
        finally
        {
            MainMenu.conexion.CerrarConexion();
        }
    }
    
    
    
    private void AgregarServicio()
    {
        double monto=0;
        try
        {
            monto=(double)Math.round(Double.valueOf(tfMonto.getText())*100)/100;

        }
        catch(Exception ex)
        {
            
        }
        if (monto>0 )
        {
            ComboBox servicio = null;
            servicio =((ComboBox) cbArticulos.getSelectedItem());
            if (servicio!=null)
            {
                Vector v= new Vector(5);            
                v.addElement(servicio.getId()); //idservicio
                v.addElement(servicio.getNombre()); //nombre
                v.addElement(monto); //monto
                _modeloTabla.addRow(v);
                _total +=monto;
                tfTotal.setText(String.valueOf(_total));
                tfTotal.repaint();
            }
            else
                JOptionPane.showMessageDialog(this, "Debe seleccionar un servicio valido","Error",JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            JOptionPane.showMessageDialog(this, "Debe Ingresar un monto valido","Error",JOptionPane.ERROR_MESSAGE);
            tfMonto.setText("");
            tfMonto.requestFocus();
        }
    }
    
    private void EliminarServicio()
    {
        int  _rowSeleccionada = _tabla.getSelectedRow();
        if(_rowSeleccionada!=-1)
        {
            int answer=JOptionPane.showConfirmDialog(null, "¿Desea eliminar la fila seleccionada?", "Confirme por favor", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(answer == JOptionPane.YES_OPTION)
            {
                double monto =(double)_tabla.getValueAt(_rowSeleccionada, 2);
                tfMonto.setText(String.valueOf(monto));
                cbArticulos.setSelectedItem(_tabla.getValueAt(_rowSeleccionada,0)); //selecciona el articulo en el combo
                _modeloTabla.removeRow(_rowSeleccionada);
                _total -=(monto);
                tfTotal.setText(String.valueOf(_total));
                tfTotal.repaint();
            }
        }
    }
    
    
    private void Guardar()
    {
        if (ValidarDetalle())
        {
            int answer=JOptionPane.showConfirmDialog(null, "¿Se creara el cargo, desea continuar?", "Confirme por favor", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(answer == JOptionPane.YES_OPTION)
            {
                try 
                {
                    if(jRadioButtonAlumno.isSelected())//individual
                    {
                        if (cbAlumno.getSelectedItem()!=null)
                        {
                            int idalumno =((ComboBox) cbAlumno.getSelectedItem()).getId();
                            String resultado="";
                            resultado=CrearCargoExtra(idalumno);
                            if ("".equals(resultado))
                            {
                                JOptionPane.showMessageDialog(this, "Los datos se almacenaron correctamente","Información",JOptionPane.INFORMATION_MESSAGE);
                                LimpiarVariables();
                            }
                            else
                                JOptionPane.showMessageDialog(this, resultado,"Error",JOptionPane.ERROR_MESSAGE);
                        }
                        else
                            JOptionPane.showMessageDialog(this, "Debe seleccionar un alumno","Error",JOptionPane.ERROR_MESSAGE);
                    }
                    else if (jRadioButtonGrado.isSelected())//si es por grado
                    {
                        HashMap ht= new HashMap();
                        for (int i = 0; i < _modeloTablaGrado.getRowCount(); i++) 
                        {
                            if ((boolean)_modeloTablaGrado.getValueAt(i, 2))
                            {
                                int idgrado = (int)_modeloTablaGrado.getValueAt(i, 0);   
                                ResultSet rs = MainMenu.conexion.EjecutarConsulta("select idalumno from alumnos where idgrado="+idgrado+" and debaja is null");
                                while (rs.next())
                                    ht.put(rs.getInt(1),rs.getInt(1));
                                MainMenu.conexion.CerrarConexion();
                            }
                        }
                        String resultado="";
                        for(Object idalumno: ht.values())
                        {
                            String resultadoIndividual=CrearCargoExtra((int)idalumno);
                            if (!"".equals(resultadoIndividual))
                                resultado += "\n"+resultadoIndividual;
                        }
                        if ("".equals(resultado))
                        {
                            JOptionPane.showMessageDialog(this, "Los datos se almacenaron correctamente","Información",JOptionPane.INFORMATION_MESSAGE);
                            LimpiarVariables();
                        }
                        else
                            JOptionPane.showMessageDialog(this, resultado +"\n los demas cargos se realizaron correctamente","Error",JOptionPane.ERROR_MESSAGE);                   
                    }
                }
                catch (SQLException ex) 
                {
                    Logger.getLogger(CargoExtra.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        else
            JOptionPane.showMessageDialog(this, "Debe agregar almenos un detalle","Error",JOptionPane.ERROR_MESSAGE);
    }
    
    
    private String CrearCargoExtra(int idalumno) throws SQLException
    {
        String idInscripcion;
        idInscripcion = MainMenu.conexion.EjecutarEscalar("select idinscripcion from inscripciones where idalumno="+idalumno+" and debaja is null");
        if (!"".equals(idInscripcion))
        {
            Connection _conexion=null;
            try
            {
                int idmovCuenta=0;
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                _conexion = DriverManager.getConnection("jdbc:mysql://"+MainMenu.server+"/"+MainMenu.dataBase,MainMenu.user, MainMenu.password);
                _conexion.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
                _conexion.setAutoCommit(false);//Inicia la transaccion
                for (int i = 0; i < _modeloTabla.getRowCount(); i++) 
                {
                    PreparedStatement pst = _conexion.prepareStatement("insert into detalleinscripcion(idinscripcion,idarticulo,monto,saldo,vencimiento) VALUES (?,?,?,?,?)");
                    pst.setInt(1, Integer.valueOf(idInscripcion));
                    pst.setInt(2, (int)_tabla.getValueAt(i, 0));
                    pst.setDouble(3, (double)_tabla.getValueAt(i, 2));
                    pst.setDouble(4, (double)_tabla.getValueAt(i, 2));
                    pst.setDate(5, Tools.ObtenerFechaSql(jDateFecha.getDate()));
                    pst.execute();
                }
                /*inserta el movimiento de cuenta */
                    Statement stMovCuenta = _conexion.createStatement();
                    stMovCuenta.execute("insert into movcuenta(fecha,tipo,id,idusuario,observaciones) values(now(),2,"+idInscripcion+","+MainMenu.IdUsuario+",'Cargo Extra')",Statement.RETURN_GENERATED_KEYS);
                    ResultSet rsMovCuenta = stMovCuenta.getGeneratedKeys();
                    if(rsMovCuenta.next()) 
                        idmovCuenta = rsMovCuenta.getInt(1);
                    stMovCuenta.execute("insert into detallemovcuenta(idmovcuenta,idcuenta,debe,haber) values("+idmovCuenta+",41,"+0+","+_total+")");
                    stMovCuenta.execute("insert into detallemovcuenta(idmovcuenta,idcuenta,debe,haber) values("+idmovCuenta+",8,"+_total+","+0+")");
                    /*Actualizar el saldo de las 2 cuentas CAPITAL Y CUENTAS POR COBRAR */
                    Statement st = _conexion.createStatement();
                    st.execute("update cuentas set saldo = saldo + "+_total+" where idcuenta in (8,41)");
                    _conexion.commit();
                    return "";
                }
                catch(Exception ex)
                {
                    if (_conexion != null) 
                        _conexion.rollback();
                    return ex.toString();
                }
                finally
                {
                    if (_conexion != null && !_conexion.isClosed())
                    {
                        _conexion.close();
                        _conexion=null;
                        System.gc();
                    }
                }
            }
        else
            return "El alumno "+ MainMenu.conexion.EjecutarEscalar("select nombre from alumnos where idalumno="+idalumno)+" no se encuentra inscrito ";
    }

    private boolean ValidarDetalle()
    {
        if (_modeloTabla.getRowCount()>0)
            return true;
        return false;
    }
    
    private void LimpiarVariables()
    {
        ObtenerFechaActual();
        CargarComboAlumno();
        CargarComboArticulos();
        _idAlumno=0;
        _total=0;
        tfTotal.setText("0.00");
        tfMonto.setText("0.00");
        _modeloTabla.RemoveAllRows();
        CargarTablaGrados();
        jRadioButtonAlumno.setSelected(true);
    }
    
    private void AddArticulo()
    {
        QuickAddArticulo d = new QuickAddArticulo(null, true,"",0);
        d.setVisible(true);  
        if (d._id!=0 && d._nombre!="")
        {
            CargarComboArticulos();
            ComboBox grado = new ComboBox(d._id,d._nombre);
            cbArticulos.getModel().setSelectedItem(grado);
        }
        d.dispose();
        System.gc();
    }
    
    private void EditaArticulo()
    {
        ComboBox grado = ((ComboBox) cbArticulos.getSelectedItem());
        QuickAddArticulo d = new QuickAddArticulo(null, true,grado.getNombre(),grado.getId());
        d.setVisible(true);  
        if (d._id!=0 && d._nombre!="")
        {
            CargarComboArticulos();
            ComboBox gradoNew = new ComboBox(d._id,d._nombre);
            cbArticulos.getModel().setSelectedItem(gradoNew);
        }
        d.dispose();
        System.gc();
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        panelEncabezado = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jDateFecha = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        cbAlumno = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jRadioButtonAlumno = new javax.swing.JRadioButton();
        jRadioButtonGrado = new javax.swing.JRadioButton();
        panelPie = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        btnNuevo = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        tfTotal = new javax.swing.JTextField();
        panelTabla = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        cbArticulos = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        btnEliminar1 = new javax.swing.JButton();
        btnAgregar = new javax.swing.JButton();
        tfMonto = new javax.swing.JTextField();
        btnAddGrado = new javax.swing.JButton();
        btnAddGrado1 = new javax.swing.JButton();
        PanelGrados = new javax.swing.JPanel();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Venta");

        jLabel1.setText("Fecha Vencimiento");

        jLabel4.setText("Alumno");

        buttonGroup1.add(jRadioButtonAlumno);
        jRadioButtonAlumno.setSelected(true);
        jRadioButtonAlumno.setText("Por Alumno");

        buttonGroup1.add(jRadioButtonGrado);
        jRadioButtonGrado.setText("Por Grado");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButtonAlumno)
                    .addComponent(jRadioButtonGrado))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jRadioButtonAlumno)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButtonGrado))
        );

        javax.swing.GroupLayout panelEncabezadoLayout = new javax.swing.GroupLayout(panelEncabezado);
        panelEncabezado.setLayout(panelEncabezadoLayout);
        panelEncabezadoLayout.setHorizontalGroup(
            panelEncabezadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelEncabezadoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelEncabezadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(panelEncabezadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cbAlumno, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jDateFecha, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
                .addGap(50, 50, 50)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(587, Short.MAX_VALUE))
        );
        panelEncabezadoLayout.setVerticalGroup(
            panelEncabezadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelEncabezadoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelEncabezadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelEncabezadoLayout.createSequentialGroup()
                        .addGroup(panelEncabezadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cbAlumno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(panelEncabezadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jDateFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jLabel5.setText("Observaciones");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/add24.png"))); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/floppy24.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btnNuevo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnNuevo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGuardar)
                .addContainerGap(50, Short.MAX_VALUE))
        );

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel9.setText("Total");

        tfTotal.setEditable(false);
        tfTotal.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        tfTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addComponent(tfTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(230, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(tfTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(47, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout panelPieLayout = new javax.swing.GroupLayout(panelPie);
        panelPie.setLayout(panelPieLayout);
        panelPieLayout.setHorizontalGroup(
            panelPieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPieLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addGroup(panelPieLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(127, 127, 127)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        panelPieLayout.setVerticalGroup(
            panelPieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPieLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelPieLayout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(panelPieLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelPieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addGroup(panelPieLayout.createSequentialGroup()
                                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))))
        );

        panelTabla.setLayout(new java.awt.CardLayout());

        jLabel6.setText("Servicio");

        jLabel7.setText("Monto");

        btnEliminar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/delete24.png"))); // NOI18N
        btnEliminar1.setText("Eliminar");
        btnEliminar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminar1ActionPerformed(evt);
            }
        });

        btnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/add24.png"))); // NOI18N
        btnAgregar.setText("Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        tfMonto.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfMonto.setText("0.00");

        btnAddGrado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/add24.png"))); // NOI18N
        btnAddGrado.setToolTipText("Nuevo");
        btnAddGrado.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        btnAddGrado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddGradoActionPerformed(evt);
            }
        });

        btnAddGrado1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/edit24.png"))); // NOI18N
        btnAddGrado1.setToolTipText("Editar");
        btnAddGrado1.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        btnAddGrado1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddGrado1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addGap(71, 71, 71)
                .addComponent(cbArticulos, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAddGrado)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAddGrado1)
                .addGap(14, 14, 14)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tfMonto, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnAgregar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnEliminar1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(334, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAddGrado1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAddGrado, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(cbArticulos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel7)
                        .addComponent(tfMonto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnAgregar)
                        .addComponent(btnEliminar1)))
                .addGap(0, 12, Short.MAX_VALUE))
        );

        PanelGrados.setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelEncabezado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelPie, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(panelTabla, javax.swing.GroupLayout.PREFERRED_SIZE, 502, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(PanelGrados, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelEncabezado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelTabla, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                    .addComponent(PanelGrados, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panelPie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
//        LimpiarVariables();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        Guardar();
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        AgregarServicio();
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnEliminar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminar1ActionPerformed
        EliminarServicio();
    }//GEN-LAST:event_btnEliminar1ActionPerformed

    private void btnAddGradoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddGradoActionPerformed
        AddArticulo();
    }//GEN-LAST:event_btnAddGradoActionPerformed

    private void btnAddGrado1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddGrado1ActionPerformed
        EditaArticulo();
    }//GEN-LAST:event_btnAddGrado1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel PanelGrados;
    private javax.swing.JButton btnAddGrado;
    private javax.swing.JButton btnAddGrado1;
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnEliminar1;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbAlumno;
    private javax.swing.JComboBox cbArticulos;
    private com.toedter.calendar.JDateChooser jDateFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JRadioButton jRadioButtonAlumno;
    private javax.swing.JRadioButton jRadioButtonGrado;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JPanel panelEncabezado;
    private javax.swing.JPanel panelPie;
    private javax.swing.JPanel panelTabla;
    private javax.swing.JTextField tfMonto;
    private javax.swing.JTextField tfTotal;
    // End of variables declaration//GEN-END:variables
}
